package events

import (
	"meman/sys/basic"
	"github.com/jinzhu/gorm"
	"math"
)



func Count(tableName string, conds map[string]interface{}) (*gorm.DB, int64){
	var count int64
	db := basic.Read()
	db.Table(tableName).Where(conds).Count(&count)
	return db, count
}

func GetPageStart(page, PerPageNums int) int {
	start := int(math.Max(float64(page-1), 0)) * PerPageNums
	return start
}

/**
@param value ,要查询的值
@param key   关键字 primary key
@param tableName 数据库名称
 */
func Exists(value, key , tableName string) (bool, error) {
	db := basic.Write()
	rows, err := db.Table(tableName).Select(key).Where(key+" = ?", value).Rows()
	if err != nil {
		return false,err
	}
	return rows.Next(),nil
}